//
//  MainViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/20.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "MainViewController.h"
#import "RegisterViewController.h"


@interface MainViewController ()<UINavigationControllerDelegate>

@property(nonatomic, retain)UITextField* unameLable;//登录
@property(nonatomic, retain)UITextField* secretText;//密码
@property(nonatomic, retain)UIButton* loginBtn;//登录
@property(nonatomic, retain)UIImageView* imageView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"主页";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.delegate = self;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuBtn"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(presentLeftMenuViewController:)];
    self.imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.imageView setUserInteractionEnabled:YES];
    self.imageView.image = [UIImage imageNamed:@"Balloon"];
    [self.view addSubview:self.imageView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"IconProfile"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(registerAction:)];
    /*
    AVObject *post = [AVObject objectWithClassName:@"TestObject"];
    [post setObject:@"Hello World!" forKey:@"words"];
    [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 保存成功了！
            NSLog(@"baocun");
        }
    }];*/

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self createView];
    [self.loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createView{

//    __weak MainViewController* weakSelf = self;
    self.unameLable = [[UITextField alloc]init];
    [self.unameLable setBorderStyle:UITextBorderStyleRoundedRect];
    [self.unameLable setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
    self.unameLable.placeholder = @"输入你的用户名";
    [self.imageView addSubview:self.unameLable];
    [self.unameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.imageView.mas_centerX);
        make.bottom.equalTo(self.imageView.mas_centerY).with.offset(-60);
        make.width.equalTo(self.imageView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(30);
    }];

    self.secretText = [[UITextField alloc] init];
    [self.secretText setBorderStyle:UITextBorderStyleRoundedRect];
    [self.secretText setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]];
    self.secretText.placeholder = @"输入你的密码";
    [self.imageView addSubview:self.secretText];
    [self.secretText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.imageView.mas_centerX);
        make.top.equalTo(self.imageView.mas_centerY).with.offset(60);
        make.width.equalTo(self.imageView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(30);
    }];

    self.loginBtn = [self createButtonwith:@"登录"];
    [self.imageView addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.imageView.mas_centerX);
        make.top.equalTo(self.imageView.mas_centerY).with.offset(160);
        make.width.equalTo(self.imageView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(30);

    }];

}

-(UIButton*)createButtonwith:(NSString*)title{
    //创建收藏按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [button setBackgroundColor:[UIColor blueColor]];
    [[button layer] setMasksToBounds:YES];
    [[button layer] setCornerRadius:5.0];
    return button;
    
}

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{

   [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

-(void)registerAction:(UIBarButtonItem*)sender{

    RegisterViewController* registerView = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:registerView animated:YES];

}

-(void)loginBtnAction:(UIButton*)btn{

    NSString* uname = self.unameLable.text;
    NSString* secret = self.secretText.text;
    [MBProgressHUD showHUDAddedTo:self.imageView animated:YES];
    [AVUser logInWithUsernameInBackground:uname password:secret block:^(AVUser *user, NSError *error) {
        NSLog(@"%@",user);
        if (user != nil) {
            NSLog(@"登录成功了");
//            NSLog(@"%@",[NSThread currentThread]);
            [MBProgressHUD hideHUDForView:self.imageView animated:YES];
            [self showAlertViewWithMessage:@"登录成功"];
        } else {
            NSLog(@"%@",error);
            [MBProgressHUD hideHUDForView:self.imageView animated:YES];
            [self showAlertViewWithMessage:@"失败"];
        }
    }];

}

-(void)showAlertViewWithMessage:(NSString*)message{

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{

    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
